home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 5 / CD-ROM Today - The Disc (Issue 5)(November 1994).ISO / mac / Mac shareware / Education / RLaB / rlib / printmat.r < prev    next >
Text File  |  1994-09-21  |  3KB  |  124 lines

  1. //--------------------------------------------------------------------------------
  2. //
  3. // printmat
  4. // 
  5. // Syntax: printmat(a,anme,rlab,clab);
  6. //
  7. // This routine prints the matrix a using the title contained in the
  8. // string name and the row labels contained in rowlab and the
  9. // column labels contained in collab. Note: rowlab and collab are
  10. // vectors of strings, such as
  11. //
  12. //         rowlab[1]="alpha";
  13. //         rowlab[2]="beta";
  14. //         rowlab[3]="gamma";
  15. //
  16. //  Note: name, rowlab, and collab are optional variables.
  17. //
  18. //--------------------------------------------------------------------------------
  19.  
  20. printmat = function(a,mname,rowlab,collab)
  21. {
  22.    local(nrows,ncols,col_per_scrn,len,col,n,icol,nmax,...
  23.          i,j,k,sdum,element,ishift,narg, rl, cl, s, tmp)
  24.  
  25.    narg=0;
  26.    if (!exist (a)) { error ("printmat: must supply a matrix"); }
  27.    if (!exist (mname)) { mname = ""; }
  28.    if (!exist (rowlab)) { rl = []; else rl = rowlab; }
  29.    if (!exist (collab)) { cl = []; else cl = collab; }
  30.  
  31.    nrows=a.nr;
  32.    ncols=a.nc;
  33.  
  34. // Create row and column labels if necessary
  35.    if (rl.n == 0)
  36.    {
  37.      for (i in 1:nrows) 
  38.      {
  39.        sprintf (tmp, "%3i", i);
  40.        rl[i]="--"+ tmp +" --> ";
  41.      }
  42.    }
  43.    if (cl.n == 0)
  44.    {   
  45.      for (i in 1:ncols) { cl[i]="----"+int2str(i)+"---- "; }
  46.    }
  47.  
  48.    col_per_scrn=5;
  49.    len=12;
  50.  
  51.    if ((nrows==0)||(ncols==0)) { 
  52.         if (length (mname)) {
  53.             printf(" \n%s = \n \n",mname);
  54.             return 0;
  55.         }
  56.         printf(" \n%s \n","     [] \n");
  57.         return 0;
  58.    }
  59.  
  60.  
  61. // Print matrix name
  62.    col=1;
  63.    n = min([col_per_scrn-1,ncols-1]);
  64.    if (length (mname)) {
  65.        printf("\n%s = \n \n",mname);
  66.    }
  67.  
  68. // Print column labels
  69.    s="";
  70.    icol=0;
  71.    while (col <= ncols) {
  72.      icol=icol+1;
  73.      s="            ";
  74.      for (j in 0:n) {
  75.           ishift=12-length(cl[j+col]);
  76.           for (k in 1:ishift) {
  77.                s=s+" ";
  78.           }
  79.           s=s+cl[j+col];
  80.      }
  81.      printf("%s\n",s);
  82.  
  83. // Print Row Labels
  84.      for (i in 1:nrows) {
  85.        s=""+rl[i];
  86.        ishift=12-length(rl[i]);
  87.        for (k in 1:ishift) {
  88.             s=s+" ";
  89.        }
  90.        for (j in 0:n) {
  91.             element = a[i;col+j];
  92.             if (element == 0) {
  93.               s=s+"           0";
  94.             else if (element >= 1.0e+06) {
  95.               sdum="";
  96.               sprintf(sdum,"%12.5e",element);
  97.               s=s+sdum;
  98.             else if (element <= -1.0e+05) {
  99.               sdum="";
  100.               sprintf(sdum,"%12.5e",element);
  101.               s=s+sdum;
  102.             else if (abs(element) < 0.0001) {
  103.               sdum="";
  104.               sprintf(sdum,"%12.5e",element);
  105.               s=s+sdum;
  106.             else
  107.               sdum="";
  108.               sprintf(sdum,"%12.5f",element);
  109.               s=s+sdum;
  110.             }}}}
  111.        }
  112.        printf("%s\n",s);
  113.      }
  114.      col = col+col_per_scrn;
  115.      printf("%s"," \n");
  116.      if ((ncols-col) < n) {
  117.           n=ncols-col;
  118.      }
  119.    }
  120.  
  121. return 0;
  122.  
  123. };
  124.